package pers.test.votemanage.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.test.votemanage.controller.response.CommResponse;
import pers.test.votemanage.domain.Question;
import pers.test.votemanage.domain.dto.QuestionDTO;
import pers.test.votemanage.domain.vo.QuestionVO;
import pers.test.votemanage.exception.CommException;
import pers.test.votemanage.service.QuestionService;

@RestController
@Api(tags = "题目控制")
public class QuestionController {

    @Autowired
   private QuestionService questionService;

    @PostMapping("/createQuestion")
    @ApiOperation(value = "创建题目",notes = "创建生成题目")
    public CommResponse createQuestion(@RequestBody QuestionVO questionVO){
        //传进来的参数是VO，这里放在DTO
        //不要随便乱用建造者模式，建造者模式只有DO用就可以了
        //Service中的方法用DTO，将DTO传到service中，在service中采用建造者模式将DTO的数据拷贝到DO，然后保存到数据库
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(questionVO,questionDTO);
        //将DTO传入service
        questionService.insertQuestion(questionDTO);
        return CommResponse.success();
    }

    @GetMapping("/queryQuestion")
    @ApiOperation(value = "查询题目",notes = "根据id查询题目")
    public CommResponse<QuestionVO> queryQuestion(@RequestParam String id){
        //数据库中查出来的是DO，在Service中，将信息从DO拷贝到DTO中
        QuestionDTO question = questionService.queryQuestion(id);
        if(question == null){
            throw new CommException("9999","概题目不存在");
        }
        //在Controller中再将数据从DTO拷贝到VO中
        QuestionVO questionVO = QuestionVO.builder().id(question.getId()).content(question.getContent()).build();
        // 实际给前端返回的数据，就是VO，只是封装在了统一的resp里面
        return new CommResponse<>("0000","success",questionVO);
    }
}
